package com.samphanie.security.validation.filter;

import com.samphanie.common.enums.ResultCode;
import com.samphanie.common.utils.ApiResponse;
import com.samphanie.common.utils.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ZSY
 * @email 1451691457@qq.com
 */
@Slf4j
@Component
public class CodeAuthenticationFailureHandler implements AuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {

        ApiResponse<?> result = ApiResponse.failed(ResultCode.USER_ERROR_A0130);

        ResponseUtil.responseWriter(response, "UTF-8", HttpStatus.UNAUTHORIZED.value(), result);
    }
}
